﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using ManagerWed.Models;
using NfsBLL;
using NfsModel;
using System.Data;
using NfsCommon.Util;

namespace ManagerWed.Controllers
{

    public class AccountController : Controller
    {
        public ActionResult Index()
        {
            ViewData["hiden_type"] = CookieHelper.GetDecodeCookie("pcnfs_type");
            return View();
        }

        #region PC模糊查询
        public JsonResult query_account(int pageindex, int pagesize, string Name, string mobile)
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                int type = CookieHelper.GetDecodeCookie("pcnfs_type") != "" ? int.Parse(CookieHelper.GetDecodeCookie("pcnfs_type")) : 0;
                List<account> lists= bll.GetAccountBycondtion(pageindex, pagesize, Name, mobile,type);
                List<account> list = new List<account>();
                foreach (account act in lists)
                {
                    account ac = act;
                    ac.user_pwd = Encrypt.Encode(act.user_pwd);
                    list.Add(ac);
                }
                object pagecount = bll.GetAccountBycondtionCount(Name, mobile);
                return Json(new { MessageCode = "0", AccountList = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }

        }
        #endregion

        #region 新增和修改数据
          
        public JsonResult GetAccount(int id)
        {
            AccountBLL bll = new AccountBLL();
            account model = bll.GetModel(id);
            return Json(new {model=model},JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult OpeaAccount()
        {
            try
            {
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                string opea = Request["opea"] != null ? Request["opea"] : "";
                string Name = Request["Name"] != null ? Request["Name"] : "";
                string password = Request["password"] != null ? Request["password"] : "";
                string moile = Request["moile"] != null ? Request["moile"] : "";
                int type = Request["type"] != null ? int.Parse(Request["type"].ToString()) : 0;
                AccountBLL bll = new AccountBLL();
                account model = new account { id = id, user_name = Name, user_pwd = password, IsAdmin = 1, type = type, mobile = moile, valid = 1, time = DateTime.Now };
                bool flag;
                if (opea == "add")
                {
                    int ids;
                    flag = bll.Add(model,out ids);
                }
                else
                {
                    flag = bll.Update(model);
                }
                if (flag)
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }


        }
        #endregion

        #region 删除数据
        [HttpPost]
        public JsonResult delete()
        {
            try
            {
                AccountBLL bll=new AccountBLL();
                int id=Request["id"]!=""?int.Parse(Request["id"]):0;
                if (bll.com_delelte(id, "t_account", "valid", 0))
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else {

                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }
        
        
        }
        #endregion
    }
}